import {Component, EventEmitter, Output} from '@angular/core';
import {IonicPage, NavController, NavParams} from 'ionic-angular';
import {SystemService} from "../../service/SystemService";
import {UserManager} from "../../../framework/manager/UserManager";
import {Log} from "../../../framework/utils/Log";
import {SLLocalStorageUtils} from "../../../framework/utils/SLLocalStorageUtils";
import {ApplyTabPage} from "../apply-tab/apply-tab";
import {CloseAccountTabPage} from "../close-account-tab/close-account-tab";
import {ApplyStoreSLS} from "../applyStoreSLS";
import {ApplyInfoTabPage} from "../apply-info-tab/apply-info-tab";

/**
 * Generated class for the AssistTabPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */


@Component({
    selector: 'page-assist-tab',
    templateUrl: 'assist-tab.html',
    providers: [SystemService]
})
export class AssistTabPage {
    private static TAG: string = 'AssistTabPage';
    dataList: Array<any> = [];
    @Output() childEvent = new EventEmitter<any>();
    zcsxId: string;
    danjuId: string;
    private editBool: boolean=false;
    private editInfo:any;
    private moneyAppleBool: boolean;
    private moneyAppleInfo: any;
    constructor(public navCtrl: NavController, public navParams: NavParams, public ss: SystemService) {
        this.zcsxId = this.navParams.data.sxId;
        //this.danjuId = UserManager.getInstance().user.djId;
        this.danjuId =this.navParams.data.danJuBH;
        this.editBool = this.navParams.data.editBool;
        this.editInfo = this.navParams.data.editInfo;
        this.moneyAppleBool = this.navParams.data.moneyAppleBool;

        this.moneyAppleInfo = this.navParams.data.moneyAppleInfo;
    }

    ngOnInit() {
        console.log('ngOnInit AssistTabPage');
    }
    ionViewDidEnter(){
        if(ApplyStoreSLS.getInstance().assistSxId==this.zcsxId && ApplyStoreSLS.getInstance().assistBool && !this.editBool){
            this.dataList = ApplyStoreSLS.getInstance().assistList;
        }else {
            if(this.moneyAppleBool){
                this.moneyApplyDataFun();
            }else {
                this.getAssistList();
            }

        }
    }
    moneyApplyDataFun(){
        this.dataList = this.moneyAppleInfo.data.FZSXlist;
        Log.printObj(AssistTabPage.TAG,"资金申请单辅助申请单信息=====",this.dataList);
        this.dataList.map((v, i) => {
            this.dataList[i].key = v.FZSXText;

        });

    }
    getAssistList() {
        let data = {
            ZhiChuSXBH: this.zcsxId,
            DanJuBH: this.danjuId
        };
        this.ss.getAssistInfo(data).subscribe((res: any) => {
            if (res.msgCode == '1' && res.list.length > 0) {
            ApplyStoreSLS.getInstance().assistBool =true;
                this.dataList = res.list;
                if (this.editBool) {
                    Log.printObj(AssistTabPage.TAG,"辅助编辑====",this.editInfo.FuZhuXX);
                    this.dataList.map((v, i) => {
                        this.dataList[i].key = '';
                    });
                    this.editInfo.FuZhuXX.map((val,n)=>{
                        this.dataList[n].key = val.FuZhuXZ;
                        this.dataList[n].MingCheng = val.FuZhuXMC;
                    })

                }else {
                    this.dataList.map((v, i) => {
                        this.dataList[i].key = '';
                    });
                }

                Log.printObj(AssistTabPage.TAG, 'result==', this.dataList);
            }
        }, error => {
        })
    }

    async next() {
        let newArr: any = [];
        if(this.moneyAppleBool){
            this.dataList.map(value => {
                newArr.push({
                    FuZhuSXBH: value.FZSXBianHao,
                    FuZhuSXZ: value.key
                });
            });
        } else {
            this.dataList.map(value => {
                newArr.push({
                    FuZhuSXBH: value.BianHao,
                    FuZhuSXZ: value.key
                });
            });
        }
        try {
            await SLLocalStorageUtils.getInstance().setAssist(newArr);
            Log.printObj(AssistTabPage.TAG, 'new Array result==', newArr);
            //this.childEvent.emit({'name': '结算信息', 'id': 3});
            this.navCtrl.push(CloseAccountTabPage,{editInfo:this.editInfo,editBool:this.editBool,moneyAppleBool:this.moneyAppleBool,moneyAppleInfo:this.moneyAppleInfo})
        } catch (error) {
            Log.printObj(AssistTabPage.TAG, "error == ", error);
            throw error;
        }
    }

    pre() {
        //this.childEvent.emit({'name': '支出信息', 'id': 1});
        this.dataStore();
        this.navCtrl.pop();
    }
    ionViewDidLeave(){
        this.dataStore();
    }
    dataStore(){
        ApplyStoreSLS.getInstance().assistSxId =this.zcsxId;
        ApplyStoreSLS.getInstance().assistList = this.dataList;
    }

}
